package com.pw.study.server.tcp;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: linux_future
 * @since: 2022/4/2
 **/
public class ServerV1 {
    Map<String, Socket> mapSockets = new HashMap<>();
    private final Socket webSocket = null;

    public static void main(String[] args) throws IOException {
        new ServerV1().init(6666);
    }

    /**
     * 首先启动8888端口客户端，再启动其他的
     */
    private void init(int port) throws IOException {
        ServerSocket server = new ServerSocket(port);
        System.out.println("server running .....");
        Socket webSocket = server.accept();
        for (; ; ) {
            Socket socket = server.accept();
            SocketAddress address = socket.getRemoteSocketAddress();
            System.out.println("webApp:" + webSocket.getRemoteSocketAddress() + " socket: "+address);
            Thread thread = new Thread(new MessageHandler(socket, webSocket));
            thread.setDaemon(true);
            thread.start();
        }

    }




}
